#include <iostream>
#include <string.h>
#include <string>
using namespace std;

class Solution 
{
public:
    string convert(string s, int numRows) {
        int size = 2 * s.size();
        int row = 0;
        if (s.size() < numRows)
            return s;
        char **p = NULL;
        p = new char* [numRows];
        for (int i = 0; i < numRows; i++)
            p[i] = new char [size / numRows];
        int j = -1, q = 0;
        //create array
        for(int i = 0; i < s.size(); i++)
        {
            start:
            if ( (q + 1)%2 == 0)
                j += 2;
            else
                j++;
        
            if (j >= numRows)
            {
                j = -1;
                q++;
                goto start;
            }
            p[j][q] = s[i];
        }
        s.clear();
        for (int i = 0; i < numRows; i++)
        {
            for(j = 0; j < size / numRows; j++)
            {
                if (p[i][j] != 0)
                    s.push_back(p[i][j]);
            }
        }
        if (p)
        {
            for (int i = 0; i < numRows; i++)
                delete [] p[i];
            delete [] p;
        }
        return s;
    }
};

int main()
{
    Solution s;
    cout << s.convert("abcdefghijklm", 3) << endl;
    cout << s.convert("A", 3) << endl;
    return 0;
}
